<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <a-row>
        <div :class="advanced ? null: 'fold'" >
            <a-col :span="12" >
              <a-form-item
                label="流程名称"
                :labelCol="{span: 4}"
                :wrapperCol="{span: 10, offset: 2}">
                 <a-input v-model="queryParams.name"/>
              </a-form-item>
            </a-col>
          <a-col :span="12" push="10">
            <a-button type="primary" @click="search">查询</a-button>
            <a-button @click="reset">重置</a-button>
            <a-button type="primary" @click="showFlowableModaler">流程定义设计</a-button>
          </a-col>
        </div>
        </a-row>
      </a-form>
    </div>
    <div>
      <div class="operator">
        <!-- <a-button type="primary" ghost @click="add" >新增</a-button> -->
        <!-- <a-button @click="batchDelete" >删除</a-button> -->
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.id"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :scroll="{ x: 900 }"
               @change="handleTableChange">
        <template slot="operation" slot-scope="text, record">
          <!-- <a-icon  type="setting" theme="twoTone" twoToneColor="#4a9ff5" @click="edit(record)" title="编辑"></a-icon> -->
          <!-- <a  @click="edit(record)" title="编辑">编辑</a>
          &nbsp; -->
           <a  @click="config(record)" title="流程配置">流程配置</a>
          &nbsp;
          <a  @click="publish(record)" title="发布">发布</a>
          <!-- &nbsp;
          <a  @click="cusForm(record)" title="挂起">挂起</a>
          &nbsp;
            <a  @click="cusForm(record)" title="激活">激活</a>
          &nbsp; -->
        </template>
      </a-table>
    </div>
    <!-- 流程配置 -->
    <process-config
      ref="ProcessConfig"
      :ProcessConfigVisiable="ProcessConfig.visiable"
      @close="handleProcessConfigClose"
      @success="handleProcessConfigSuccess">
    </process-config>
    <FlowableFrame ref="FlowableFrame" @close="reset" @success="search"></FlowableFrame>
  </a-card>
</template>

<script>
import ProcessConfig from './ProcessConfig'
import FlowableFrame from './FlowableFrame'

export default {
  name: 'Process',
  components: {ProcessConfig, FlowableFrame},
  data () {
    return {
      // 数据初始区域
      advanced: false,
      ProcessConfig: {
        visiable: false
      },
      queryParams: {},
      sortedInfo: null,
      paginationInfo: null,
      dataSource: [],
      loading: false,
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      }
    }
  },
  computed: {
    columns () {
      return [
        {
          title: '流程编号',
          dataIndex: 'id'
        }, {
          title: '流程名称',
          dataIndex: 'name'
        }, {
          title: '创建时间',
          dataIndex: 'created'
        }, {
          title: '更新时间',
          dataIndex: 'lastUpdated'
        }, {
          title: '最新发布版本',
          dataIndex: 'version',
          customRender: (version) => {
            return 'V' + version
          }
        }, {
          title: '最近发布时间',
          dataIndex: 'deployTime'
        }, {
          title: '最新流程发布状态',
          dataIndex: 'status',
          customRender: (status) => {
            switch (status) {
              case '0':
                return <a-tag color="red">未发布</a-tag>
              case '1':
                return <a-tag color="green">已发布</a-tag>
              default:
                return status
            }
          }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }]
    }
  },
  mounted () {
    this.fetch()
  },
  methods: {
    showFlowableModaler () {
      this.$refs.FlowableFrame.show()
    },
    publish (record) {
      this.$post('process/design/deploy', {id: record.id
      }).then((r) => {
        this.$message.success('发布成功')
        this.search()
      })
    },
    config (record) {
      this.ProcessConfig.visiable = true
      this.$refs.ProcessConfig.setFormValues(record)
    },
    handleProcessConfigClose () {
      this.ProcessConfig.visiable = false
    },
    handleProcessConfigSuccess () {
      this.ProcessConfig.visiable = false
      this.search()
    },
    search () {
      let {sortedInfo, filteredInfo} = this
      let sortField, sortOrder
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field
        sortOrder = sortedInfo.order
      }
      this.fetch({
        sortField: sortField,
        sortOrder: sortOrder,
        ...filteredInfo
      }, this.queryParams)
    },
    reset () {
      // 重置分页
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }
      // 重置列过滤器规则
      this.filteredInfo = null
      // 重置列排序规则
      this.sortedInfo = null
      // 重置查询参数
      this.queryParams = {}
      this.fetch()
    },
    handleTableChange (pagination, filters, sorter) {
      // 将这三个参数赋值给Vue data，用于后续使用
      this.paginationInfo = pagination
      this.filteredInfo = filters
      this.sortedInfo = sorter
      this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters
      }, this.queryParams)
    },
    fetch (params = {}, querypas) {
      // 显示loading
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$get('process/design', {
        ...params, ...querypas
      }).then((r) => {
        let data = r.data
        const pagination = { ...this.pagination }
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
        // 数据加载完毕，关闭loading
        this.loading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
@import "../../../../static/less/Common";
</style>
